<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="showAddDialog"
                >新增角色</el-button
              >
            </el-row>
            <!-- 表格 -->
            <el-table :data="rolesList">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template v-slot="scope">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="showEditDialog(scope.row)"
                    >编辑</el-button
                  >
                  <el-button
                    size="small"
                    type="danger"
                    @click="hdel(scope.row.id)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>

            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                layout="prev,pager,next"
                :page-size="pageParams.pagesize"
                :total="total"
                :current-page="pageParams.page"
                @current-change="currentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>

    <el-dialog
      :visible.sync="showDialog"
      :title="isEdit ? '编辑角色' : '新增部门'"
      @close="doClose"
    >
      <el-form
        label-width="80px"
        :rules="formRules"
        :model="formData"
        ref="formRef"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="formData.name"></el-input>
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="formData.description"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="doCancel">取消</el-button>
          <el-button type="primary" @click="doSubmit">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getRoles, delRole, addRole, editRole } from "@/api/settings";

export default {
  data() {
    return {
      rolesList: [],
      pageParams: {
        page: 1,
        pagesize: 2,
      },
      total: 1,
      formData: {
        name: "",
        description: "",
      },
      isEdit: false,
      showDialog: false,
      formRules: {
        name: [
          { required: true, message: "角色名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.loadRoles();
  },
  methods: {
    async loadRoles() {
      try {
        const res = await getRoles(this.pageParams);
        // console.log(res);
        this.rolesList = [...res.data.rows];
        this.total = res.data.total;
      } catch (err) {
        this.$message.error(err.message);
      }
    },
    currentChange(curPage) {
      // console.log(curPage);
      this.pageParams.page = curPage;
      this.loadRoles();
    },
    hdel(id) {
      this.$confirm("此操作将会永久删除数据,是否继续", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 删除成功
          this.dodel(id);
        })
        .catch(() => {
          // 取消删除
          this.$message.info("已取消删除");
        });
    },
    async dodel(id) {
      try {
        if (this.rolesList.length === 1) {
          this.pageParams.page--;
          this.pageParams.page = Math.max(1, this.pageParams.page);
        }
        const res = await delRole(id);
        console.log(res);
        this.$message.success(res.message);
        this.loadRoles();
      } catch (err) {
        this.$message.error(err.message);
      }
    },
    showAddDialog() {
      this.showDialog = true;
      this.isEdit = false;
    },
    showEditDialog(data) {
      this.showDialog = true;
      this.isEdit = true;
      this.formData = { ...data };
    },
    doSubmit() {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.isEdit ? this.doEdit() : this.doAdd();
        }
      });
    },
    async doAdd() {
      try {
        const res = await addRole(this.formData);
        console.log(res);
        this.$message.success(res.message);
        this.total++;
        this.pageParams.page = Math.ceil(this.total / this.pageParams.pagesize);
        this.loadRoles();
        this.showDialog = false;
      } catch (err) {
        this.$message.error(err.message);
      }
    },
    async doEdit() {
      try {
        const res = await editRole(this.formData);
        console.log(res);
        this.$message.success(res.message);
        this.loadRoles();
        this.showDialog = false;
      } catch (err) {
        this.$message.error(err.message);
      }
    },
    doClose() {
      this.$refs.formRef.resetFields();
    },
    doCancel() {
      this.showDialog = false;
    },
  },
};
</script>
